package com.ruoyi.lxy.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.lxy.service.ITRouteService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lxy.domain.vo.TProjectVo;
import com.ruoyi.lxy.domain.bo.TProjectBo;
import com.ruoyi.lxy.service.ITProjectService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("lxy/project")
public class TProjectController extends BaseController {

    private final ITProjectService iTProjectService;
    private final ITRouteService routeService;
    /**
     * 查询项目列表
     */
    @SaCheckPermission("lxy:project:list")
    @GetMapping("list")
    public TableDataInfo<TProjectVo> list(TProjectBo bo, PageQuery pageQuery) {
        switch(LoginHelper.getLoginUser().getRoles().get(0).getRoleName()) {
            case "旅行社":
                return iTProjectService.TravellagenqueryPageList(bo, pageQuery);
            case "管理员":
                return iTProjectService.queryPageList(bo, pageQuery);
            case "教职工":

        }
        return iTProjectService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取项目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("lxy:project:query")
    @GetMapping("/{id}")
    public R<TProjectVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iTProjectService.queryById(id));
    }

    /**
     * 新增项目
     */
    @SaCheckPermission("lxy:project:add")
    @Log(title = "项目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TProjectBo bo) {
        return toAjax(iTProjectService.insertByBo(bo));
    }

    /**
     * 修改项目
     */
    @SaCheckPermission("lxy:project:edit")
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TProjectBo bo) {
        return toAjax(iTProjectService.updateByBo(bo));
    }

    /**
     * 删除项目
     *
     * @param ids 主键串
     */
    @SaCheckPermission("lxy:project:remove")
    @Log(title = "项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        for (Long id : ids) {
            if (routeService.CheckRouteExist(id)) {
                return R.warn("该项目下存在路线，无法删除");
            }
        }
        return toAjax(iTProjectService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
















//    /**
//     * 获取项目名字
//     *
//     * @param id 主键
//     */
//    @SaCheckPermission("lxy:project:query")
//    @GetMapping("/getProjectName/{id}")
//    public R<TProjectVo> getProjectName(@NotNull(message = "主键不能为空")
//                                 @PathVariable Long id) {
//        return R.ok(iTProjectService.queryById(id).getName());
//    }
}
